package cm.iccsoft.android.telecomcost.ui;

import java.util.ArrayList;
import java.util.Calendar;

import cm.iccsoft.android.telecomcost.DatabaseManager;
import cm.iccsoft.android.telecomcost.R;
import cm.iccsoft.android.telecomcost.TelecomCost;
import cm.iccsoft.android.telecomcost.activities.DailyThresholdListActivity;
import cm.iccsoft.android.telecomcost.entities.DailyThreshold;
import cm.iccsoft.android.telecomcost.entities.MonthlyThreshold;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class DailyThresholdDialog extends Dialog {

	private DatabaseManager db;
	private TextView lblMonday;
	private TextView lblTuesday;
	private TextView lblWednesday;
	private TextView lblThursday;
	private TextView lblFriday;
	private TextView lblSaturday;
	private TextView lblSunday;
	private EditText txtMonday;
	private EditText txtThuesday;
	private EditText txtWednesday;
	private EditText txtThursday;
	private EditText txtFriday;
	private EditText txtSaturday;
	private EditText txtSunday;
	private Button btnCloseDailyThresholdDialog1;
	private Button btnThursdaySunday;
	private Button btnCloseDailyThresholdDialog2;
	private Button btnSaveDailyThreshold;
	private int viewIndex = 0;
	private boolean shouldRefreshThresholdList = false;
	private ViewFlipper flipper;
	private Context _context;
	private Button btnMonday_Wednesday;

	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public DailyThresholdDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		_context = context;
	}

	/**
	 * @param context
	 * @param theme
	 */
	public DailyThresholdDialog(Context context, int theme) {
		super(context, theme);
		_context = context;
	}

	public DailyThresholdDialog(Context context) {
		super(context);
		_context = context;
	}

	private void saveDailyThreshold() throws Exception {		
		Log.i("DailyThresholdDialog", "saving DailyThreshold...");		
		
		DailyThresholdListActivity activity = (DailyThresholdListActivity) _context;
		ArrayList<DailyThreshold> thresholds = activity.getThresholds();

		Log.i("DailyThresholdDialog", "we load the DailyThreshold first...");
		DailyThreshold threshold = db.getDailyThreshold(Calendar.MONDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtMonday.getText().toString().equals("") ? 0 : Double.parseDouble(txtMonday.getText().toString()));		
		db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());
		
		threshold = db.getDailyThreshold(Calendar.TUESDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtThuesday.getText().toString().equals("") ? 0 : Double.parseDouble(txtThuesday.getText().toString()));
		db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());
		
		threshold = db.getDailyThreshold(Calendar.WEDNESDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtWednesday.getText().toString().equals("") ? 0 : Double.parseDouble(txtWednesday.getText().toString()));
		db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());
		
		threshold = db.getDailyThreshold(Calendar.THURSDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtThursday.getText().toString().equals("") ? 0 : Double.parseDouble(txtThursday.getText().toString()));
		db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());	
		
		threshold = db.getDailyThreshold(Calendar.FRIDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtFriday.getText().toString().equals("") ? 0 : Double.parseDouble(txtFriday.getText().toString()));
		db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());
		
		threshold = db.getDailyThreshold(Calendar.SATURDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtSaturday.getText().toString().equals("") ? 0 : Double.parseDouble(txtSaturday.getText().toString()));
		db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());	
		
		threshold = db.getDailyThreshold(Calendar.SUNDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		threshold.setAmount(txtSunday.getText().toString().equals("") ? 0 : Double.parseDouble(txtSunday.getText().toString()));
		MonthlyThreshold monthlyLimit = db.saveDailyThreshold(threshold,activity.getWeek(),activity.getMonth());	

		activity.setMonthlyThreshold(monthlyLimit);
		shouldRefreshThresholdList = true;
		Log.i("DailyThresholdDialog", "daily threshold saved...");
		Toast.makeText(_context, "Daily threshold saved !!!", 3).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("DailyThresholdDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		db = TelecomCost.getDatabaseManager();
		setContentView(R.layout.daily_threshold_dialog);

		setTitle("Daily Threshold");

		flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
		lblMonday = (TextView) findViewById(R.id.lblMonday);
		lblTuesday = (TextView) findViewById(R.id.lblThuesday);
		lblWednesday = (TextView) findViewById(R.id.lblWednesday);
		lblThursday = (TextView) findViewById(R.id.lblThursday);
		lblFriday = (TextView) findViewById(R.id.lblFriday);
		lblSaturday = (TextView) findViewById(R.id.lblSaturday);
		lblSunday = (TextView) findViewById(R.id.lblSunday);
		txtMonday = (EditText) findViewById(R.id.txtMonday);
		txtThuesday = (EditText) findViewById(R.id.txtThuesday);
		txtWednesday = (EditText) findViewById(R.id.txtWednesday);
		txtThursday = (EditText) findViewById(R.id.txtThursday);
		txtFriday = (EditText) findViewById(R.id.txtFriday);
		txtSaturday = (EditText) findViewById(R.id.txtSaturday);
		txtSunday = (EditText) findViewById(R.id.txtSunday);
		btnCloseDailyThresholdDialog1 = (Button) findViewById(R.id.btnCloseDailyThresholdDialog1);
		btnThursdaySunday = (Button) findViewById(R.id.btnThursdaySunday);
		btnMonday_Wednesday = (Button) findViewById(R.id.btnMonday_Wednesday);
		btnCloseDailyThresholdDialog2 = (Button) findViewById(R.id.btnCloseDailyThresholdDialog2);
		btnSaveDailyThreshold = (Button) findViewById(R.id.btnSaveDailyThreshold);

		btnThursdaySunday.setText(DateUtils.getDayOfWeekString(Calendar.THURSDAY,
				DateUtils.LENGTH_SHORT) + " - " + DateUtils.getDayOfWeekString(Calendar.SUNDAY,
						DateUtils.LENGTH_SHORT) + " >>");
		
		btnMonday_Wednesday.setText("<< " + DateUtils.getDayOfWeekString(Calendar.MONDAY,
				DateUtils.LENGTH_SHORT) + " - " + DateUtils.getDayOfWeekString(Calendar.WEDNESDAY,
						DateUtils.LENGTH_SHORT));
		
		lblTuesday.setText(DateUtils.getDayOfWeekString(Calendar.TUESDAY,
				DateUtils.LENGTH_LONG));
		lblWednesday.setText(DateUtils.getDayOfWeekString(Calendar.WEDNESDAY,
				DateUtils.LENGTH_LONG));
		lblThursday.setText(DateUtils.getDayOfWeekString(Calendar.THURSDAY,
				DateUtils.LENGTH_LONG));
		lblFriday.setText(DateUtils.getDayOfWeekString(Calendar.FRIDAY,
				DateUtils.LENGTH_LONG));
		lblSaturday.setText(DateUtils.getDayOfWeekString(Calendar.SATURDAY,
				DateUtils.LENGTH_LONG));
		lblSunday.setText(DateUtils.getDayOfWeekString(Calendar.SUNDAY,
				DateUtils.LENGTH_LONG));
		lblMonday.setText(DateUtils.getDayOfWeekString(Calendar.MONDAY,
				DateUtils.LENGTH_LONG));

		btnThursdaySunday.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				showThursdaySundayView();
			}
		});

		btnMonday_Wednesday.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				showMondayWednesdayView();
			}
		});

		btnCloseDailyThresholdDialog1
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View arg0) {
						closeDialog();
					}
				});

		btnCloseDailyThresholdDialog2
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View arg0) {
						closeDialog();
					}
				});

		btnSaveDailyThreshold.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				try {
					saveDailyThreshold();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Log.i("DailyThresholdDialog", "Dialog ohne bundle created....");
	}

	protected void closeDialog() {
		if (shouldRefreshThresholdList) {
			((DailyThresholdListActivity) _context).bindData();
		}
		if(viewIndex == 1)
		{
			viewIndex--;
			flipper.showPrevious();
		}
		
		hide();
	}

	protected void showThursdaySundayView() {
		viewIndex++;
		flipper.showNext();
	}

	protected void showMondayWednesdayView() {
		viewIndex--;
		flipper.showPrevious();
	}

	@Override
	public void show() {
		Log.i("DailyThresholdDialog", "showing the dialog...");
		super.show();
		Log.i("DailyThresholdDialog", "dialog on screen...");
		bindDailyTreshold();
	}

	@Override
	protected void onStart() {
		Log.i("DailyThresholdDialog", "Starting the dialog...");
		super.onStart();
		Log.i("DailyThresholdDialog", "dialog started...");
	}

	private void bindDailyTreshold() {
		Log.i("DailyThresholdDialog", "binding DailyThresholdDialog...");

		DailyThresholdListActivity activity = (DailyThresholdListActivity) _context;
		ArrayList<DailyThreshold> thresholds = activity.getThresholds();

		DailyThreshold threshold = db.getDailyThreshold(Calendar.MONDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(),
				thresholds);
		txtMonday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getDailyThreshold(Calendar.TUESDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(), thresholds);
		txtThuesday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getDailyThreshold(Calendar.WEDNESDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(), thresholds);
		txtWednesday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getDailyThreshold(Calendar.THURSDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(), thresholds);
		txtThursday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getDailyThreshold(Calendar.FRIDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(), thresholds);
		txtFriday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getDailyThreshold(Calendar.SATURDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(), thresholds);
		txtSaturday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));

		threshold = db.getDailyThreshold(Calendar.SUNDAY,activity.getWeek().getWeekOfMonth(),activity.getMonth().getMonth(), thresholds);
		txtSunday.setText((CharSequence) (threshold == null ? 0 : Double
				.toString(threshold.getAmount())));
	}

	@Override
	protected void onStop() {
		Log.i("DailyThresholdDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("DailyThresholdDialog", "dialog stopped...");
	}
}
